
Ext.define('Model.TipoRol', {
    extend: 'Ext.data.Model',
    fields: [ {
			name: 'tipoRol'
		}]
}); 

Ext.define('administracion.RolesForm', {  extend: 'Ext.form.Panel',  alias: 'widget.writerform',   requires: ['Ext.form.field.Text'],

    initComponent: function(){
        
        Ext.apply(this, {
            activeRecord: null,  iconCls: 'icon-rol',  frame: true,   title: 'Roles',  defaultType: 'textfield',  bodyPadding: 20,
            fieldDefaults: { anchor: '100%',},
            items: [{	fieldLabel: 'Id'
						,name: 'id',
						type: 'int',	
						hidden: true,
					},{	fieldLabel: '<b>Nombre Rol</b>',
						itemId: 'f_nombre',
						name: 'nombreRol',fieldStyle: 'background-color: transparent; border: none; background-image: none;',
						allowBlank: false
					},{	
						
						fieldLabel: '<b>Descripcion</b>',
						itemId: 'f_descripcion',
						fieldStyle: 'background-color: transparent; border: none; background-image: none;',
						name: 'descripcion',}
					,{
						xtype: 'combobox',
						name: 'tipoRol',
						//value: 'Ninguno',
						fieldLabel: '<b>Tipo Plantilla</b>',
						displayField: 'tipoRol',
						fieldStyle: 'background-color: #fff; border: 1px dotted;',
						hidden: true,
						//disabled: true,
						itemId: 'f_plantilla',
						store: Ext.create('Ext.data.Store', {
								model: 'Model.TipoRol',
								autoLoad: true,
								proxy: {
									type: 'ajax',
									url: 'listarTipoRol',
									reader: {
										type: 'json',
										successProperty: 'success',
										root: 'data',
										messageProperty: 'message'
									}
								}        
								
							}),
					   
					}],
					dockedItems: [{
									xtype: 'toolbar',
									items: [
									{	iconCls: 'icon-add',	
										itemId: 'create',	
										text: '<b>Crear</b>',
										tooltip:'Crear un rol nuevo',	
										//disabled: true,
										hidden: true,									
										scope: this,	
										handler: this.onCreate
									},{	iconCls: 'icon-save',
									itemId: 'save',
									text: '<b>Guardar</b>',
									tooltip:'Modificar el rol seleccionado',
									hidden: true,
									//disabled: true,
									scope: this, 	
									handler: this.onSave
								},{	iconCls: 'icon-delete',
									itemId: 'delete',
									hidden: true,
									disabled: true,	
									tooltip:'Eliminar el rol seleccionado',
									scope: this,
									text: '<b>Eliminar</b>',	
									handler: this.onDelete
								},{	iconCls: 'icon-user',
									itemId: 'users',
									text: '<b>Usuarios</b>',
									tooltip:'Asignar - Desasignar Usuarios al rol seleccionado',	
									hidden: true,
									disabled: true,
									scope: this, 	
									handler: this.onUsuarios
								},{
									iconCls: 'icon-permiso',
									itemId: 'permisos',	
									text: '<b>Permisos</b>',
									tooltip:'Asignar - Desasignar Permisos al rol seleccionado',
									hidden: true,	
									scope: this,
									disabled: true, 
									handler: this.onPermisos
								},{}]
								},{
									weight: 1,
									xtype: 'toolbar',	
									dock: 'bottom',		
									items: ['->', {
									iconCls: 'icon-tipoItem',
									itemId: 'tipoItem',
									text: '<b>Tipos Item</b>',
									tooltip:'Asignar - Desasignar TiposItem al rol seleccionado',
									hidden: true,
									scope: this,
									disabled: true, 
									handler: this.onTiposItem
								},{
									iconCls: 'icon-fase',
									itemId: 'fase',
									text: '<b>Fases</b>',
									tooltip:'Asignar - Desasignar Fases al rol seleccionado',
									hidden: true,
									disabled: true,
									scope: this, 
									handler: this.onFases
								},{
									iconCls: 'icon-proyecto',
									itemId: 'proyecto',
									tooltip:'Asignar - Desasignar Proyectos al rol seleccionado',
									text: '<b>Proyectos</b>',
									hidden: true,
									disabled: true,
									scope: this, 
									handler: this.onProyectos
								},{
									iconCls: 'icon-editar',
									itemId: 'editar',
									text: '<b>Editar</b>',
									disabled: true,
									hidden: true,
									scope: this, 
									disabled: true,
									handler: this.onEditar
								},{
									iconCls: 'icon-add',
									itemId: 'nuevo',
									text: '<b>Nuevo</b>',
									scope: this,
									disabled: true,
									handler: this.onNuevo
								},{	iconCls: 'icon-reset',	
										text: '<b>Reset</b>',	
										tooltip:'Resetear el formulario',	
										scope: this,	
										handler: this.onReset
									}]
							}]	});
				this.callParent();
    },

    setActiveRecord: function(record){
        this.activeRecord = record;
        if (record) { 			
            if (!this.down('#editar').isDisabled()){
				this.down('#editar').show();			
			}			
            if (!this.down('#delete').isDisabled()){
				this.down('#delete').show();			
			}
            if (!this.down('#users').isDisabled()){
				this.down('#users').show();			
			}  
            if (!this.down('#permisos').isDisabled()){
				this.down('#permisos').show();			
			}
            if (!this.down('#tipoItem').isDisabled()){
				this.down('#tipoItem').show();			
			}
            if (!this.down('#fase').isDisabled()){
				this.down('#fase').show();			
			}  
            if (!this.down('#proyecto').isDisabled()){
				this.down('#proyecto').show();			
			}			
            this.down('#create').hide();
            this.down('#save').hide();
			this.down('#nuevo').hide();
			this.down('#f_plantilla').hide(); 
			this.down('#f_nombre').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_descripcion').setFieldStyle('background-color: transparent; border: none; background-image: none;');      
			this.getForm().loadRecord(record);
        } else {
			this.down('#editar').hide();  
			this.down('#delete').hide();    
			this.down('#users').hide();   
			this.down('#permisos').hide();
            this.down('#tipoItem').hide();
            this.down('#proyecto').hide();
            this.down('#fase').hide();			
            if (!this.down('#nuevo').isDisabled()){
				this.down('#nuevo').show();			
			}
			this.down('#create').hide();
			this.down('#save').hide();
			this.down('#f_plantilla').hide();
			this.down('#f_nombre').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_descripcion').setFieldStyle('background-color: transparent; border: none; background-image: none;');  			
			this.getForm().reset();
        }
    },

    onNuevo: function(){
		this.getForm().reset();
		this.down('#create').show();
		this.down('#nuevo').hide(); 
		this.down('#f_nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_descripcion').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_plantilla').show();

				
    },

    onEditar: function(){
		this.down('#users').hide();
		this.down('#delete').hide();
		this.down('#editar').hide();
		this.down('#permisos').hide();
		this.down('#save').show();
		this.down('#f_nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_descripcion').setFieldStyle('background-color: #fff; border: 1px dotted;');
		
    },  
        
    onSave: function(){
        var form = this.getForm();
                    form.submit({                        
                        url: 'modificarRol',                        
                        params: {	submit: 'submit'	},
						method: "post",
                        success: function(form, action) {
									form.reset();
									Ext.Msg.show({	title: '<br><p align="center"><b>ALPHA MANAGER</b></p>',   
													msg: 'Rol modificado satisfactoriamente',	icon: 'ext-mb-ok',
									});		this.fireEvent('refresh');
						}, scope: this,
                        failure: function(form, action) {								
									Ext.Msg.show({	title: '<br><p align="center"><b>ALPHA MANAGER</b></p>',   
													msg: 'No se ha podido modificar el rol', 	icon: 'ext-mb-error',
									});
                        }                                                   
                    });                    				
    },
    
    onCreate: function(){
        var form = this.getForm();
                    form.submit({                        
                        url: 'crearRol',
                        params: {	submit: 'submit'	},
						method: "post",
                        success: function(form, action) {
								form.reset();
								Ext.Msg.show({	title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
												msg: 'Rol creado satisfactoriamente', //<- El mensaje  
												icon: 'ext-mb-ok',
								});		this.fireEvent('refresh');                           
                        }, scope: this,                     
                        failure: function(form, action) {
									Ext.Msg.show({	title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
													msg: 'No se ha podido crear el rol', 	icon: 'ext-mb-error',
									});
                        }
                    });
    },

    onDelete: function(){ 
        var form = this.getForm();
                    form.submit({                        
                        url: 'eliminarRol',
                        params: {	submit: 'submit'	},
						method: "post",
                        success: function(form, action) {
								form.reset();
								Ext.Msg.show({ 		title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
													msg: 'Rol eliminado satisfactoriamente', 	icon: 'ext-mb-ok',
								});		this.fireEvent('refresh');                           
                        },  scope: this,                       
                        failure: function(form, action) {
									Ext.Msg.show({	title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
													msg: 'No se ha podido eliminar el rol', 	icon: 'ext-mb-error',
									});
                        }
                    });
    },
    
	onUsuarios: function(){
			var form = this.getForm();
			this.fireEvent('ddUsuarios', form.getValues(true));            
	},
	
	onPermisos: function(){
			var form = this.getForm();
			this.fireEvent('ddPermisos', form.getValues(true));            
	},
		
	onTiposItem: function(){
			var form = this.getForm();
			this.fireEvent('ddTiposItem', form.getValues(true));            
	},	

	onFases: function(){
			var form = this.getForm();
			this.fireEvent('ddFases', form.getValues(true));            
	},
	
	onProyectos: function(){
			var form = this.getForm();
			this.fireEvent('ddProyectos', form.getValues(true));            
	},
	
    onReset: function(){
        this.setActiveRecord(null);
        this.getForm().reset();
        this.fireEvent('refresh'); 
    }    
});
